package com.cncytech.safe_space_config_test;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;


public class MainActivity extends AppCompatActivity {

    private TextView latitudeTextView;
    private TextView longitudeTextView;
    private LocationManager locationManager;
    Handler handler = new Handler();
    private static final String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        latitudeTextView = findViewById(R.id.latitudeTextView);
        longitudeTextView = findViewById(R.id.longitudeTextView);
        location();
    }




    public void location() {
        // 检查定位权限
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // 申请定位权限
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION}, 1);
        }
        // 获取LocationManager实例
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // 注册LocationListener来接收位置更新

        /**
         * （1）GPS_PROVIDER：通过 GPS 来获取地理位置的经纬度信息；优点：获取地理位置信息精确度高；缺点：只能在户外使用，获取经纬度信息耗时，耗电；
         * （2）NETWORK_PROVIDER：通过移动网络的基站或者 Wi-Fi 来获取地理位置；优点：只要有网络，就可以快速定位，室内室外都可；缺点：精确度不高；
         * （3）PASSIVE_PROVIDER：被动接收更新地理位置信息，而不用自己请求地理位置信息。
         * PASSIVE_PROVIDER 返回的位置是通过其他 providers 产生的，可以查询 getProvider() 方法决定位置更新的由来，需要 ACCESS_FINE_LOCATION 权限，但是如果未启用 GPS，则此 provider 可能只返回粗略位置匹配；
         */
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, new LocationListener() {
            @Override
            public void onLocationChanged(@NonNull Location location) {
                // 当位置更新时调用
                double latitude = location.getLatitude();
                double longitude = location.getLongitude();

                handler.post(()->{
                     latitudeTextView.setText("纬度:" + latitude);
                    longitudeTextView.setText("经度:" + longitude);
                });

                Log.i(TAG, "onLocationChanged: " + location.getProvider());
                Toast.makeText(MainActivity.this, "位置更新"+location.getLatitude(), Toast.LENGTH_SHORT).show();

            }

            @Override
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // 当GPS状态变化时调用
                Log.i(TAG, "onStatusChanged: ");
                Toast.makeText(MainActivity.this, "GPS状态变化", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onProviderEnabled(String provider) {
                // 当GPS可用时调用
                Log.i(TAG, "onProviderEnabled: ");
                Toast.makeText(MainActivity.this, "GPS可用", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onProviderDisabled(String provider) {
                // 当GPS不可用时调用
                Log.i(TAG, "onProviderDisabled: ");
                Toast.makeText(MainActivity.this, "GPS不可用", Toast.LENGTH_LONG).show();

            }
        });
    }

    public void ReadSMS(View view) {
        Intent intent = new Intent(this, ReadSMSActivity.class);
        startActivity(intent);
    }

    public void readContact(View view) {
        Intent intent = new Intent(this, ReadContactActivity.class);
        startActivity(intent);
    }

    public void readCallLog(View view) {
        Intent intent = new Intent(this, ReadCallLogActivity.class);
        startActivity(intent);
    }

    public void readPhoneNumber(View view) {
        Intent intent = new Intent(this, ReadPhoneNumberActivity.class);
        startActivity(intent);
    }

    public void sendSMS(View view) {
        Intent intent = new Intent(this, SendSMSActivity.class);
        startActivity(intent);
    }

    public void sendBlueToothData(View view) {
        Intent intent = new Intent(this, BluetoothSendDataActivity.class);
        startActivity(intent);
    }

    public void shoot(View view) {
        Intent intent = new Intent(this, ShootActivity.class);
        startActivity(intent);
    }

    public void recordAudio(View view) {
        Intent intent = new Intent(this, RecordAudioActivity.class);
        startActivity(intent);
    }
}
